package com.baifenhao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class OnlyNumAndText {
	private static List<String> listUrl = null;
	private static String urlStr;
	private static int i;

	private static String s1 = "";
	private static String s2 = "";
	private static String s3 = "";
	private static File file = new File("C:\\Users\\Administrator\\Desktop\\小芳姐的excel\\streamExcel百分号和无符号.xls");

	private static String firstLink;

	public static void main(String[] args) throws IOException {
		if(!file.exists()){
			HSSFWorkbook workBook1 = new HSSFWorkbook();
			HSSFSheet sheet1 = workBook1.createSheet("疾病记录");
			HSSFRow titleRow1 = sheet1.createRow(0);
			HSSFCell[] cellArray = new HSSFCell[17];
			for (int i = 0; i < cellArray.length; i++) {
				cellArray[i] = titleRow1.createCell(i);
			}
			cellArray[0].setCellValue("OMIM号");
			cellArray[1].setCellValue("疾病名称");
			cellArray[2].setCellValue("TEXT");
			FileOutputStream out = new FileOutputStream(file, true);
			workBook1.write(out);
			out.flush();
			out.close();
		}
		
		
		Connection conn = null;
		Statement stat = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection(
					"jdbc:mysql:///diseaseoflinkcapture111-2", "root", "root");
			stat = (Statement) conn.createStatement();
			ResultSet rs = stat
					.executeQuery("select * from t_nextlink order by id DESC limit 1");
			rs.next();
			System.out.println(rs.getRow());
			if(rs.getRow() > 0) {
				String nextLinkName = rs.getString("nextLinkName");
				
				normalReadPage("http://www.omim.org/search?index=entry&search=prefix:%25%20AND%20chromosome_group:A&sort=number%20asc&start=1&limit=10", nextLinkName);
			} else if(rs.getRow()==0) {
				//normalReadPage("http://www.omim.org/search?index=entry&search=prefix:%23%20AND%20chromosome:X&sort=number%20asc&start=1&limit=10","http://www.omim.org/search?index=entry&search=prefix:%23%20AND%20chromosome:X&sort=number%20asc&start=1&limit=10");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void normalReadPage(String firstStr, String urlStr) {
		Connection conn = null;
		Statement stat = null;
		try {
			URL url = new URL(urlStr.toString());
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			InputStreamReader input = new InputStreamReader(
					httpConn.getInputStream(), "UTF-8");
			BufferedReader buffReaderRead = new BufferedReader(input);
			String str1 = "";
			listUrl = new ArrayList<String>();
			listUrl.add("");
			while ((str1 = buffReaderRead.readLine()) != null) {
				if (!(str1.trim().equals(""))) {
					if (str1.contains("<a href=\"/entry/")) {
						str1 = "http://www.omim.org" + str1.substring(str1.indexOf("\"/") + 1, str1.indexOf("\">"));
						str1 = str1.replaceAll(" ", "%20");
						List<String> list = StringUtil.findTenDiseaseXiaofangjie();
						if (!list.contains(str1)) {
							findEndPage(str1);
							Class.forName("com.mysql.jdbc.Driver");
							conn = (Connection) DriverManager.getConnection("jdbc:mysql:///diseaseoflinkcapture111-2","root", "root");
							stat = (Statement) conn.createStatement();
							stat.execute("INSERT INTO t_diseaselink(firstLinkName,diseaseLinkName) VALUE('" + firstStr + "','" + str1 + "')");
						} else{
							System.out.println("在抓取已经有的");
						}
					} else if (str1.toString().contains("\">Next")) {
						str1 = "http://www.omim.org" + str1.substring(str1.indexOf("\"/") + 1, str1.indexOf(">Next") - 1);
						str1 = str1.replaceAll(" ", "%20");
						if (listUrl != null) {
							if (listUrl.contains(str1.toString())) {
								listUrl.add(str1);
								Class.forName("com.mysql.jdbc.Driver");
								conn = (Connection) DriverManager.getConnection("jdbc:mysql:///diseaseoflinkcapture111-2","root", "root");
								stat = (Statement) conn.createStatement();
								stat.execute("INSERT INTO t_nextlink(firstLinkName,nextLinkName) VALUE('"+ firstStr + "','" + str1 + "')");
								normalReadPage(firstStr, str1.toString());
							} else {
								listUrl.add(str1);
							}
						}
					}
				}
			}
			buffReaderRead.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}  finally {
			
		}
	}
	/**
	 * 疾病的页面信息，进行截字符串存数据库导出excel表
	 * @param strEndUrl
	 */
	public static void findEndPage(String strEndUrl) {
		long startTime = new Date().getTime();
		
		try {
			URL url = new URL(strEndUrl.toString());
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			InputStreamReader input = new InputStreamReader(httpConn.getInputStream(), "UTF-8");
			BufferedReader buffReader = new BufferedReader(input);
			String str2 = "";
			s1 = "";
			s2 = "";
			s3 = "";
			while ((str2 = buffReader.readLine()) != null) {
				if (!str2.trim().equals("")) {
					if (str2.contains("mim-number")) {//
						str2 = buffReader.readLine();
						str2 = StringUtil.getSupString(str2);
						s1 = str2.trim();
					} else if (str2.contains("preferred-title")) {
						str2 = buffReader.readLine();
						str2 = StringUtil.getSupString(str2);
						s2 = str2.trim();
					} else if (str2.contains("<tr><td class=\"section-title text-font\"><a name=\"text\">")) {
						for (int i = 0; i < 20; i++) {
							str2 = buffReader.readLine();
							if(!"".equals(str2.trim())){
								if((str2.contains("<tr><td class=\"section-text text-font lookup\" >"))&&(str2.contains("<a name=\"clinicalFeatures\">"))){
									s3 = "";
								}else if((str2.contains("<tr><td class=\"section-text text-font lookup\" >"))&&(!str2.contains("<a name=\"clinicalFeatures\">"))){
									str2 = str2.substring(str2.indexOf("<tr><td class=\"section-text text-font lookup\" >")+"<tr><td class=\"section-text text-font lookup\" >".length(), str2.lastIndexOf("</td></tr>"));
									str2 = StringUtil.getSupString(str2);
									s3 = str2;
								}
								break;
							}
						}
						
							
					} 
				}
			}
			buffReader.close();
			
			HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(file));
			HSSFSheet sheet = workBook.getSheet("疾病记录");
			
			HSSFRow titleRow = sheet.getRow(sheet.getLastRowNum());
			titleRow = sheet.createRow(sheet.getLastRowNum() + 1);
			titleRow.createCell(0).setCellValue(s1.trim().toString());
			titleRow.createCell(1).setCellValue(s2.trim().toString());
			titleRow.createCell(2).setCellValue(s3.trim().toString());
			FileOutputStream out = new FileOutputStream(file);
			out.flush();
			workBook.write(out);
			out.close();
			System.out.println(s1+"一个用时:"+(new Date().getTime()-startTime)+"毫秒");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 

	}


	public static String getUrlStr() {
		return urlStr;
	}

	public static void setUrlStr(String urlStr) {
		OnlyNumAndText.urlStr = urlStr;
	}

	public static int getI() {
		return i;
	}

	public static void setI(int i) {
		OnlyNumAndText.i = i;
	}

	public static String getFirstLink() {
		return firstLink;
	}

	public static void setFirstLink(String firstLink) {
		OnlyNumAndText.firstLink = firstLink;
	}
}
